package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.Warning;
import com.siwei.mes.mapper.system.WarningMapper;
import com.siwei.mes.service.system.WarningService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 预警表(Warning)表服务实现类
 *
 * @author dmwang
 * @since 2024-07-18 09:51:52
 */
@Service
public class WarningServiceImpl implements WarningService {
    @Resource
    private WarningMapper warningMapper;

    @Override
    public Long create(Warning entity) {
        warningMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Warning entity) {
        Warning warning = warningMapper.selectByPrimaryKey(entity.getId());
        assertTrue(warning == null, "预警表信息不存在");
        entity.init();
        warningMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Warning warning = warningMapper.selectByPrimaryKey(id);
        assertTrue(warning == null, "预警表信息不存在");
        warningMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Warning> getPage(PageQuery<Warning, Warning> query) {
        Warning searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> warningMapper.getList(searchParam));
    }

    @Override
    public Warning loadById(Long id) {
        return warningMapper.loadById(id);
    }

    @Override
    public List<Warning> getAll(Warning entity) {
        return warningMapper.getList(entity);
    }
}
